; *******************************
; Netflix Control v0.8
; *******************************
;
; AutoHotKey script for control of Netflix: Movie Viewer in Internet Explorer

#SingleInstance force

; Initialize window group
ViewerWindowTitle := "Netflix"
FullScreenTitle := "ahk_class AGFullScreenWinClass"
MPCWindowTitle := "ahk_class MediaPlayerClassicW"

nscExe := A_ScriptDir . "\netflix_silverlight_control.exe"

SetTitleMatchMode, 2
DetectHiddenWindows On
if WinExist("NetflixSilverlightControl")
{
    ; Window exists... do nothing.
}
else
{
    ; The actual control script isn't running.  Run it.
    Run, %nscExe%
}
DetectHiddenWindows Off

GroupAdd, ViewerWindow, %ViewerWindowTitle%
GroupAdd, ViewerWindow, %MPCWindowTitle%

; ****************
; START Of Hotkeys
; ****************
; Here you can edit/cchange the keys used for the various actions.
; "^e::" for example, is control + e.  "Media_Stop::" is the stop button
; found on most multimedua keyboards.
; see: http://www.autohotkey.com/docs/KeyList.htm for more
; each action can have multiple keys to do the same thing.  Just list them one after another.


; Exit the netflix movie viewer, and wait
; for a new viewer window to open
; ******************************************
;#IfWinExist, ahk_group ViewerWindow
#IfWinActive, FullScreenTitle
^e::
Media_Stop::
Browser_Stop::
Browser_Home::
    result := SendCtrlMsg(10, 100)
return


; Jump ahead about 10 pixels in the timeline
; ******************************************
;#IfWinExist, ahk_group ViewerWindow
;#IfWinExist, FullScreenTitle
;^s::
;s::
;^Right::
;MsgBox, , test, test
;result := SendCtrlMsg(10, 101)
;return


; Jump back about 10 pixels in the timeline
; ******************************************
;#IfWinExist, ahk_group ViewerWindow
;#IfWinActive, FullScreenTitle
;^a::
;a::
;^Left::
;    result := SendCtrlMsg(10, 102)
;return


; Move to next Chapter/Episode
; *****************************
#IfWinActive, ahk_group ViewerWindow
^c::
^f::
Media_Next::
#IfWinActive, FullScreenTitle
^c::
^f::
Media_Next::
    result := SendCtrlMsg(10, 103)
return

; Move to previous Chapter/Episode
; *********************************
#IfWinActive, ahk_group ViewerWindow
^b::
Media_Prev::
#IfWinActive, FullScreenTitle
^b::
Media_Prev::
    result := SendCtrlMsg(10, 104)
return


; Play
; *****************************
;#IfWinExist, ahk_group ViewerWindow
#IfWinActive, FullScreenTitle
V::
    result := SendCtrlMsg(10, 105)
return


; Pause
; *****************************
;#IfWinExist, ahk_group ViewerWindow
#IfWinActive, FullScreenTitle
^l::
    result := SendCtrlMsg(10, 106)
return


; Toggle play/pause
; *****************************
;#IfWinExist, ahk_group ViewerWindow
#IfWinActive, FullScreenTitle
Media_Play_Pause::
Space::
Enter::
^p::
    result := SendCtrlMsg(10, 107)
return


; Toggle Mute On/Off
; *****************************
;#IfWinExist, ahk_group ViewerWindow
#IfWinActive, FullScreenTitle
Volume_Mute::
F8::
    result := SendCtrlMsg(10, 108)
return


; Move volume Up
; *****************************
;#IfWinExist, ahk_group ViewerWindow
#IfWinActive, FullScreenTitle
Volume_Up::
F10::
    result := SendCtrlMsg(10, 109)
return


; Move volume down
; *****************************
;#IfWinExist, ahk_group ViewerWindow
#IfWinActive, FullScreenTitle
Volume_Down::
F9::
    result := SendCtrlMsg(10, 110)
return

; Fullscreen
; *****************************
#IfWinExist, ahk_class AGFullScreenWinClass
F11::
    ;result := SendCtrlMsg(10, 112)
    WinActivate, %FullScreenTitle%
    SendInput, {Esc}
return

#IfWinExist, ahk_group ViewerWindow
F11::
    IfWinExist, %FullScreenTitle%
    {
        return
    }
    ifWinExist, %MPCWindowTitle%
    {
        WinActivate, %MPCWindowTitle%
        SendInput, {F11}
        return
    } Else IfWinNotActive, %ViewerWindowTitle%
    {
        WinActivate, %ViewerWindowTitle%
    }
    result := SendCtrlMsg(10, 111)
    ControlFocus, NativeWindowClass1, %ViewerWindowTitle%
    ControlSend, NativeWindowClass1, fff, %ViewerWindowTitle%
    ControlFocus, NativeWindowClass1, %ViewerWindowTitle%
    WinActivate, ahk_class AGFullScreenWinClass
    
return

#IfWinActive, ahk_group ViewerWindow
F12::
    result := SendCtrlMsg(10, 113)
return


; *******************
; END Of Hotkeys
; *******************

SendCtrlMsg(wParam, lParam)
{
    SentMessage := 0
    SetTitleMatchMode 2
    
        DetectHiddenWindows On
    
        if WinExist("NetflixSilverlightControl")
        {
            PostMessage, 0x5555, wParam, lParam  ; The message is sent  to the "last found window" due to WinExist() above.
            SentMessage := 1
        }
        else
        {    
            SentMessage := 0
            ;MsgBox, No Sent Msg
        }

    DetectHiddenWindows Off  ; Must not be turned off until after PostMessage.
    Return SentMessage
}